//==========================================================================
// Copyright (c) 2000-2007,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <elastos.h>
#include <stdio.h>
#include <stdlib.h>
#include "dbg-share.h"

using namespace Elastos;

int share_number(int n)
{
    ISharedMemory *pShaMem = NULL;
    Address pMem;
    ECode ec = CSystem::FindRunningObject(AGENT_SHARE_MEM, (IInterface **)&pShaMem);
    if (NOERROR != ec) {
        return AGENT_SHARE_ERROR;
    }
    ec = pShaMem->Attach(MemoryProtection_RW, &pMem);
    if (NOERROR != ec) {
        return AGENT_SHARE_ERROR;
    }
    sprintf((char *)pMem, ":%d", n);
    
    ec = pShaMem->Detach();
    if (NOERROR != ec) {
        return AGENT_SHARE_ERROR;
    }
    IEvent *pEvent = NULL;
    ec = CSystem::FindRunningObject(AGENT_SYNC_EVENT, (IInterface **)&pEvent);
    if (NOERROR != ec) {
        ec = CEvent::New(AGENT_SYNC_EVENT, true, 0, &pEvent, NULL);
        if (NOERROR != ec) {
            return AGENT_SHARE_ERROR;
        }
    }
    pEvent->Notify(1);
    pEvent->Release();
    return AGENT_SHARE_OK;
}

int main(int argc, char *argv[])
{
	if (argc != 2) {
		printf("Usage: ... <port>\r\n");
		return 1;
	}
	share_number(atoi(argv[1]));
    return 0;
}
